package com.bancomer.batch.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.apache.log4j.Logger;

public class LinuxCommand {
	
	
	public static String execute(String command, boolean showError) throws Exception{
		Logger logger = Logger.getLogger("LinuxCommand");
		StringBuffer output = new StringBuffer();
		 
		Process p;
	
		p = Runtime.getRuntime().exec(command);
		BufferedReader reader;
		if(showError == true){
			reader = new BufferedReader(new InputStreamReader(p.getErrorStream()));
		}
		else{
			reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
		}
			
		String line = "";		
		while ((line = reader.readLine())!= null) {
			logger.info(line);
			output.append(line + "\n");
				
		}
		if(output.toString().contains("[main] ERROR")){
			throw new Exception();
		}
		return output.toString();
		
	}
	
	public static void main(String[] args) throws Exception {
		args[0]=args[0].replace("$", " ");
		System.out.println("****LinuxCommand:");
		System.out.println("command:"+args[0]);
		System.out.println(LinuxCommand.execute(args[0],true));	
		
	}

}
